Run Python in a flow
Some image checks are easiest to write in Python, with OpenCV and NumPy. The «python-executor» node runs your Python code on each message, like a function node runs JavaScript, and hands it the image ready for OpenCV. In this guide you give the node a Python environment of its own with the packages it needs, and write a focus check: a number that drops when the camera's picture is blurred.
What you will learn
- How to create a Python environment for your flows and install packages in it
- How to run Python code on the images of a flow
- How to turn a Rosepetal image into an OpenCV image
- How to keep Python running between messages so that it answers fast
Before you start
Here two inject nodes, «in focus» and «out of focus», send the path of a photo of a pack label on the controller, /tmp/line-check/label-sharp.jpg or /tmp/line-check/label-blurred.jpg, and an rp-image-in node loads it into msg.payload and shows it under itself; see Load and transform an image. The debug node «sharpness» shows msg.sharpness. Installing packages needs the controller to reach the Python package index (pypi.org).
1. Add a Python node
Type python in «filter nodes», drag a «python executor» node between the image node and the debug node and wire it to both. Open it. «Environment» is the Python the node runs: a virtual environment with packages of its own. While it is empty, the node runs the controller's own Python, python3 in «Python Path», with whatever packages are installed there, and the editor marks the node as unfinished.

2. Add an environment
«Environment» reads «Add new python-environment...», in red while it is empty: click the «+» next to it. A second dialog opens, for the environment's configuration node. It checks the controller's Python first and shows its version and its path: the environment is built from it, as a Python virtual environment (venv).
An environment keeps its packages apart from the controller's own Python and from other environments, so two flows can use different versions of a package, and installing one never breaks the controller.

3. Create the environment
Write a «Name», focus-check here, and press «Create Environment». The controller creates the virtual environment in ~/.node-red/python-envs/focus-check and updates pip inside it; the dialog says «Environment created». The folder is created at once, without a deploy. Its name is fixed from then on.

4. Install the packages
In «Install», write opencv-python-headless and press «Install». pip downloads it into the environment, with NumPy, which it needs, and shows its output as it goes; then the list of «Installed Packages» shows both with their versions.
Write several packages separated by spaces, and a version as pip takes it: numpy==1.26.4. The trash button of a package uninstalls it. Packages are installed at once, without a deploy.

5. Use it in the node
Click «Add». Back in the python executor, «Environment» shows focus-check and «Python Path» is gone: the node runs the environment's own Python. Every python executor can use the same environment; open it again with the pencil to add packages later.

6. Write the check
Replace the example code with:
import cv2
gray = cv2.cvtColor(rp_to_cv(msg["payload"]), cv2.COLOR_BGR2GRAY)
# Sharp edges make the Laplacian vary a lot; a blurred picture keeps it flat
msg["sharpness"] = round(cv2.Laplacian(gray, cv2.CV_64F).var())
return msgThe code runs once per message, with the message as msg, and must return msg. rp_to_cv turns a Rosepetal image into a NumPy array in OpenCV's BGR order, and rp_from_cv turns one back, to send a processed image on. The variance of the Laplacian is a classic focus measure: sharp edges give a high value.

7. Keep Python running
Tick «Hot Mode». Without it, every message starts Python again and imports OpenCV, which takes longer than the check itself. In hot mode the Python process stays alive, images reach it through shared memory, and «Workers» sets how many messages it handles at once. «Preload Imports» runs imports once when it starts.

8. Deploy
Click «Done» and «Deploy». The node starts its Python and says «ready». A python executor without an «Environment» still runs, with the controller's Python, but the editor marks it with a red triangle and asks you to «Confirm deploy» every time; with an environment it deploys straight away.

9. Check a sharp picture
Open «Debug messages» with the bug button at the bottom right of the editor, and press «in focus». The debug panel shows msg.sharpness, a high value for a picture in focus.

10. Check a blurred picture
Press «out of focus». The same label out of focus gives a much lower value. Pick a limit between the two with pictures of your own line, and a switch node on msg.sharpness can warn the operators when the camera loses focus.

Good to know
- What the code can use.
msgis a dictionary;flow_ctxandglobal_ctxread and write the flow's context, andnode.warn("…")writes to the debug panel. An exception turns the status red, shows the error in the debug panel and stops the message. - Where the environment lives. On the controller, in
~/.node-red/python-envs/(on this controller a link to/opt/storage/python-envs), in a folder named after the environment in lower case, with anything other than letters, digits,-and_turned into_. The flow only keeps the name: a flow exported to another controller needs the environment created there again, from the same dialog, with its packages. - Deleting it. «Delete Environment» in its dialog removes the folder with all its packages, at once. Deleting the configuration node in the editor, or the flow, leaves the folder on the controller.
- Another Python. An environment is always built from the controller's
python3. To use another interpreter, leave «Environment» empty and write its path in «Python Path». - Heavy JavaScript. The
worker-functionnode is the same idea for JavaScript: it runs the code of a function node in a separate thread, so a long calculation does not hold up the rest of the flows; see Find what slows a flow down.
Next steps
Recorded on Rosepetal release 1.8.15, Node-RED 5.0.10.